//
//  DyViewController.m
//  UI_豆瓣影院
//
//  Created by dllo on 16/1/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DyViewController.h"
#import "AAALHXDateHandle.h"
#import "ActiveModel.h"
#import "RootViewController.h"
#import "Root1ViewController.h"
@interface DyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation DyViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"收藏活动";
    
    
    // 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor cyanColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
    
    
}

// 行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _arr.count;
}

// 行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

// 编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text =_arr[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dataHunde1];
    
    NSString *str = _arr[indexPath.row];
    ActiveModel *model = [ActiveModel actionModel];
    model.title = str;
    Root1ViewController *vc = [[Root1ViewController alloc] init];
    
    // 回传到root1
    for (ActiveModel *model1 in _array) {
        if ([model1.title isEqualToString:model.title]) {
            
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
            CGRect rect = [model1.content boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            
            vc.heightl = rect.size.height;
            
            vc.model = model1;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
    
    
    
    
    
}
- (void)dataHunde1 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        
    }
    self.array = [NSMutableArray array];
    NSMutableArray * arr = result[@"events"];
    for (NSMutableDictionary *dic in arr) {
        ActiveModel *model = [ActiveModel actionModel];
        [model setValuesForKeysWithDictionary:dic];
        [_array addObject:model];
        
    }
    
    
}










// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    
}
// 修改删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消收藏";
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        
        [[AAALHXDateHandle sharedDateBaseHandle] deleteModel:_arr[indexPath.row]];
        
        [_arr removeObjectAtIndex:indexPath.row];
        
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        
    } else {
        
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
